package com.bnlr.lrparking.appui;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bnlr.lrparking.appui.home.HomeActivity;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.guide.Splish_PagerActivity;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;

import cn.jpush.android.api.JPushInterface;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * 欢迎页  页面入口
 *
 * @author hasee
 */
public class WelcomeActivity extends BaseActivity {

    private String id = "";
    private String splish = "";
    private TextView tv_timer;
    private boolean isclick = false;
    private RelativeLayout rele_intent;
    private GifImageView image_gif;
    private GifDrawable gifDrawable;
    private CountDownTimer countDownTimer;
    private boolean isIsclick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splish);
        splish = AppConfig.getInstance().getString("splish", "");
        image_gif = (GifImageView) findViewById(R.id.image_gif);
        tv_timer = (TextView) findViewById(R.id.tv_timer);
        rele_intent = (RelativeLayout) findViewById(R.id.relativeLayout14);
        rele_intent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setIntentNextActivity();
            }
        });
        try {
            // 如果加载的是gif动图，第一步需要先将gif动图资源转化为GifDrawable
            // 将gif图资源转化为GifDrawable
            gifDrawable = new GifDrawable(getResources(), R.mipmap.qidongye_youhua);
            // gif1加载一个动态图gif
            image_gif.setImageDrawable(gifDrawable);

        } catch (Exception e) {
            e.printStackTrace();
        }
        countDownTimer = new CountDownTimer(4000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                int i = (int) millisUntilFinished / 1000;
                tv_timer.setText(i + "");
            }

            @Override
            public void onFinish() {
                setIntentNextActivity();
            }
        };
        countDownTimer.start();

        Log.i("TAG", "WelcomeActivity start");

    }


    /**
     * 获取版本号
     *
     * @return
     */
    public String getVersion() {

        PackageManager manager = this.getPackageManager();
        PackageInfo info;
        String version = "";
        try {
            info = manager.getPackageInfo(this.getPackageName(), 0);
            version = info.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            version = "1.0";
        }
        return version;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    /**
     * 设置跳转页面
     */
    private void setIntentNextActivity() {
        isIsclick = true;
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        // 第一次登陆
        if (TextUtils.isEmpty(splish)) {
            Log.i("TAG", "WelcomeActivity go to Splish_PagerActivity!");
            AppConfig.getInstance().putString("Version",
                    getVersion());
            Intent intent = new Intent(WelcomeActivity.this,
                    Splish_PagerActivity.class);
            startActivity(intent);
            WelcomeActivity.this.finish();
        } else {                          // 已经在使用
            Log.i("TAG", "WelcomeActivity go to HomeActivity!");
            AppConfig.getInstance().putString("Version",
                    getVersion());
            startActivity(HomeActivity.class);
            this.finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (gifDrawable != null) {
            gifDrawable.stop();
        }

    }
}
